import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';

import { MessageEntity } from './message.entity';

@Entity('user')
export class UserEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, length: 20 })
  username: string;

  @Column()
  password: string;

  @Column()
  try_count: number;

  @Column({ nullable: true })
  avatar: string;

  @Column({ nullable: true })
  background: string;

  @CreateDateColumn({
    name: 'create_time',
    type: 'timestamp',
  })
  createTime: Date;

  @UpdateDateColumn({
    name: 'update_time',
    type: 'timestamp',
  })
  updateTime: Date;

  @OneToMany(() => MessageEntity, message => message.owner)
  messages?: MessageEntity[];

  constructor(username: string, password: string) {
    this.username = username;
    this.password = password;
    this.try_count = 0;
  }
}
